//
// Created by test on 23-3-4.
//
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdio.h>
#include<iostream>
#include<sys/stat.h>
#include<sys/fcntl.h>
#include<sys/uio.h>
#include<cstdlib>
#include<sys/sendfile.h>
int main(int argc,char* argv[]){
    const char* ip=argv[1];
    int port=atoi(argv[2]);
    const char* file_name=argv[3];
    sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(port);
    inet_pton(AF_INET,ip,&addr.sin_addr);
    int sock=socket(PF_INET,SOCK_STREAM,0);
    int ret=bind(sock,(struct sockaddr*)&addr,sizeof(addr));
    assert(ret!=-1);
    int filefd=open(file_name,O_RDONLY);
    struct stat file_stat;
    ret=fstat(filefd,&file_stat);
    ret=listen(sock,5);
    struct sockaddr_in client;
    socklen_t client_len=sizeof(client);
    int connfd=accept(sock,(struct sockaddr*)&client,&client_len);
    assert(connfd>=0);
    sendfile(connfd,filefd,NULL,file_stat.st_size);
    close(connfd);
    close(sock);
    return 0;
}